package com.huadao.cart_ccs.experiment.changeLiquidPlan;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseListDF;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.common.adapter.CommonRecycleAdapter;
import com.huadao.cart_ccs.common.adapter.CommonViewHolder;
import com.huadao.cart_ccs.common.adapter.FooterHolder;
import com.huadao.cart_ccs.common.adapter.HeaderHolder;
import com.huadao.cart_ccs.common.adapter.onItemCommonClickListener;
import com.huadao.cart_ccs.greendao.entity.LiquidLoopPlan;

import java.util.ArrayList;
import java.util.List;

public class ChangeLiquidPlanListFrag extends BaseListDF implements IChangeLiquidPlanListContract.IChangeLiquidPlanListView, LiquidLoopPlanDetailDF.OnSaveNewPlan {

    private PlanAdapter adapter;
    private List<LiquidLoopPlan> liquidLoopPlans;
    private ChangeLiquidPlanListPresenter planListPresenter;

    @Override
    public int getRecycleViewId() {
        return R.id.rv_plan;
    }

    @Override
    public int getSwipeRefreshLayoutId() {
        return 0;
    }

    @Override
    protected void init() {
        planListPresenter=new ChangeLiquidPlanListPresenter(this);
        liquidLoopPlans=new ArrayList<>();
        adapter=new PlanAdapter(getActivity(),liquidLoopPlans,R.layout.item_liquid_loop_plan);
        view.findViewById(R.id.btn_add_plan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LiquidLoopPlanDetailDF liquidLoopPlanDetailDF=LiquidLoopPlanDetailDF.newInstance(null);
                liquidLoopPlanDetailDF.setOnSaveNewPlan(ChangeLiquidPlanListFrag.this);
                liquidLoopPlanDetailDF.show(getChildFragmentManager(),null);
            }
        });
    }

    @Override
    protected RecyclerView.ItemDecoration setItemDecoration() {
        return null;
    }

    @Override
    protected CommonRecycleAdapter setAdapter() {
        return adapter;
    }

    @Override
    public RecyclerView.LayoutManager setLayoutManage() {
        return new LinearLayoutManager(getActivity());
    }

    @Override
    protected void loadData() {
        planListPresenter.getPlanList();
    }

    @Override
    protected void onRefreshData() {
        loadData();
    }

    @Override
    protected BasePresenter getPresenter() {
        return planListPresenter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfrag_changeliquidplan;
    }

    @Override
    public void onGetPlanList(List<LiquidLoopPlan> liquidLoopPlanList) {
        this.liquidLoopPlans.clear();
        this.liquidLoopPlans.addAll(liquidLoopPlanList);
        notifyDataSetChanged();
    }

    @Override
    public void OnSaveNewPlanSucc(LiquidLoopPlan liquidLoopPlan) {
        planListPresenter.addPlan(liquidLoopPlan);
        liquidLoopPlans.add(liquidLoopPlan);
        adapter.notifyItemInserted(liquidLoopPlans.size()-1);
        showToast("保存成功");
    }

    @Override
    public void OnEditPlanSucc(LiquidLoopPlan liquidLoopPlan) {
        showToast("修改成功");
    }

    @Override
    public void OnDeletePlanSucc(LiquidLoopPlan liquidLoopPlan) {
        planListPresenter.deletePlan(liquidLoopPlan);
        int index=liquidLoopPlans.indexOf(liquidLoopPlan);
        liquidLoopPlans.remove(index);
        adapter.notifyItemRemoved(index);
        showToast("删除成功");
    }

    class PlanAdapter extends CommonRecycleAdapter<LiquidLoopPlan>{

        private onItemCommonClickListener itemClick=new onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                LiquidLoopPlanDetailDF liquidLoopPlanDetailDF=LiquidLoopPlanDetailDF.newInstance(liquidLoopPlans.get(position));
                liquidLoopPlanDetailDF.setOnSaveNewPlan(ChangeLiquidPlanListFrag.this);
                liquidLoopPlanDetailDF.show(getChildFragmentManager(),null);
            }

            @Override
            public void onItemLongClickListener(int position) {

            }
        };

        public PlanAdapter(Context context, List<LiquidLoopPlan> dataList, int layoutId) {
            super(context, dataList, layoutId);
        }

        @Override
        protected void bindData(CommonViewHolder holder, LiquidLoopPlan data) {
            holder.setText(R.id.startDate,TimeUtil.log_sdf.format(data.getStartTime()));
            holder.setText(R.id.plan_name,"换液");
            holder.setText(R.id.amount,String.format("%dml",data.getAmount()));
            holder.setText(R.id.fluid_speed,String.format("%dr/min",data.getFluidIntake_speed()));
            holder.setText(R.id.discharge_speed,String.format("%dr/min",data.getDischard_speed()));
            holder.setCommonClickListener(itemClick);
        }

        @Override
        protected void bindFooterData(FooterHolder holder) {

        }

        @Override
        protected void bindHeaderData(HeaderHolder holder) {

        }
    }
}
